import Vue from 'vue'
import Router from 'vue-router'
import Login from "../components/Login.vue";
import Home from "../components/Home.vue";
import Register from "../components/Register.vue";
import User from "../components/User.vue";
import UserData from "../components/UserData.vue";
import Hello from "../components/Hello.vue";




Vue.use(Router)


const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Login',
      component: Login
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      children:[
        {
          path: "/user",
          name: "User",
          component:User
        },
        {
          path: "/userData",
          name: "UserData",
          component:UserData
        },
        {
          path: "/hello",
          name: "Hello",
          component:Hello
        }
      ]
    },
    {
      path: '/register',
      name: 'Register',
      component: Register
    }
  ]
})

//路由守卫
router.beforeEach((to,from,next)=>{
  console.log(sessionStorage.getItem("token"))
  if(to.path=="/"||to.path=="/register"){
    next();
  }else{
    console.log(sessionStorage.getItem("token"))
    if(sessionStorage.getItem("token")){
      next();
    }else{
      next({path:"/"})
    }
  }
})

export default router
